package top.cdmrx.demo.edu.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.cdmrx.common.R;
import top.cdmrx.demo.edu.entity.Course;
import top.cdmrx.demo.edu.entity.vo.CourseInfoForm;
import top.cdmrx.demo.edu.entity.vo.CoursePublishVo;
import top.cdmrx.demo.edu.service.CourseService;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-09-18
 */
@RestController
@RequestMapping("/edu/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    @PostMapping("saveCourseInfo")
    public R saveCourseInfo(@RequestBody CourseInfoForm courseInfoForm){

        String courseId=courseService.saveCourseInfo(courseInfoForm);
        return R.success().data("courseId",courseId);

    }

    //根据ID查询课程基本信息（大部分用于修改前的查询回显）
    @PostMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){

        CourseInfoForm courseInfoForm=courseService.getCourseInfo(courseId);
        System.out.println(courseInfoForm+"************************************");
        return R.success().data("courseInfoForm",courseInfoForm);
    }

    //真正的修改方法
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm){

        courseService.updateCourseInfo(courseInfoForm);
        return R.success();
    }
    //根据课程id查询出特定的课程对象
    @PostMapping("publishCourseInfo/{id}")
    public R publishCourseInfo(@PathVariable String id){
        CoursePublishVo publishVo=courseService.publishCourseInfo(id);
        System.out.println(publishVo);
        return R.success().data("publishVo",publishVo);
    }

    //发布课程
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        Course course = new Course();
        course.setId(id);
        course.setStatus("Normal");
        courseService.updateById(course);
        return R.success();
    }

    //课程列表
    @GetMapping
    public R listCourse(){
        List<Course> list =
                courseService.list(null);

        return R.success().data("list",list);
    }


    //删除课程方法
    @DeleteMapping("{id}")
    public R deleteCourse(@PathVariable String id){
        courseService.removeCourse(id);
        return R.success();
    }
}

